import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


public class CropTriangle {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(""
					.getClass().getResourceAsStream("/A-small-practice.in")));
			int testCaseNumber = Integer.valueOf(br.readLine());
			for (int i = 0; i < testCaseNumber; i++) {
				String[] params = br.readLine().split(" ");
				int n = Integer.valueOf(params[0]);
				int A = Integer.valueOf(params[1]);
				int B = Integer.valueOf(params[2]);
				int C = Integer.valueOf(params[3]);
				int D = Integer.valueOf(params[4]);
				int x0 = Integer.valueOf(params[5]);
				int y0 = Integer.valueOf(params[6]);
				int M = Integer.valueOf(params[7]);
				
				List<Point> points = new ArrayList<Point>();
				points.add(new Point(x0, y0));
				for(int j = 1; j < n; j++) {
					x0 = (A * x0 + B) % M;
					y0 = (C * y0 + D) % M;
					points.add(new Point(x0, y0));
				}
				List<Triangle> triangles = new ArrayList<Triangle>();
				List<Point> triangleVertexes = new ArrayList<Point>();
				getTriangles(triangles, triangleVertexes, points, 0, 3);
				removeDuplicateTriangles(triangles);
				System.out.println("Case #" + (i + 1) + ": " + triangles.size());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void removeDuplicateTriangles(List<Triangle> triangles) {
		List<Point> points = new ArrayList<Point>();
		for(int i = 0; i < triangles.size(); i++) {
			Triangle triangle = triangles.get(i);
			if(points.contains(triangle.A) || points.contains(triangle.B) || points.contains(triangle.C)) {
				triangles.remove(i);
				i--;
			} else {
				points.add(triangle.A);
				points.add(triangle.B);
				points.add(triangle.C);
			}
		}
	}

	private static void getTriangles(List<Triangle> triangles,
			List<Point> triangleVertexes, List<Point> points, int begin, int vertex) {
		if(vertex == 0) {
			Point a = triangleVertexes.get(0);
			Point b = triangleVertexes.get(1);
			Point c = triangleVertexes.get(2);
			if ((a.x + b.x + c.x) % 3 == 0 && (a.y + b.y + c.y) % 3 == 0) {
				triangles.add(new Triangle(a, b, c));
			}
		} else {
			for(int i = begin; i < points.size(); i++) {
				triangleVertexes.add(points.get(i));
				getTriangles(triangles, triangleVertexes, points, i + 1, vertex - 1);
				triangleVertexes.remove(triangleVertexes.size() - 1);
			}
		}
	}

	static class Point {
		public int x;
		public int y;
		public Point(int x, int y){
			this.x = x;
			this.y = y;
		}

		public boolean equals(Object point) {
			Point p = (Point)point;
			return x == p.x && y == p.y;
		}
		public String toString() {
			return "(" + x + ", " + y + ")";
		}
	}

	static class Triangle {
		public Point A;
		public Point B;
		public Point C;
		public Triangle(Point a, Point b, Point c) {
			A = a;
			B = b;
			C = c;
		}

		public boolean equals(Object triangle) {
			Triangle t = (Triangle)triangle;
			return A == t.A && B == t.B && C == t.C;
		}
		
		public String toString() {
			return "[" + A + ", " + B + ", " + C + "]";
		}
	}
}
